package com.bank.backend.boot.server.service.impl;


import com.bank.backend.boot.base.constant.ExceptionConstant;
import com.bank.backend.boot.base.model.dto.AccountDTO;
import com.bank.backend.boot.base.model.entity.Account;
import com.bank.backend.boot.base.model.entity.User;
import com.bank.backend.boot.base.model.exception.GlobalException;
import com.bank.backend.boot.base.model.vo.AccountVO;
import com.bank.backend.boot.server.converter.AccountConverter;
import com.bank.backend.boot.server.mapper.AccountMapper;
import com.bank.backend.boot.server.mapper.UserMapper;
import com.bank.backend.boot.server.service.AccountService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author JiaoWei
* @description 针对表【account(账户信息)】的数据库操作Service实现
* @createDate 2025-03-29 21:04:26
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
    implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private AccountConverter accountConverter;

    /**
     * 保存账户信息
     * @param accountDTO
     * @return
     */
    @Override
    public Boolean saveAccount(AccountDTO accountDTO) {
        if (null == accountDTO.getUid()) {
            throw new GlobalException(ExceptionConstant.USER_ID_NULL);
        }

        User user = userMapper.selectById(accountDTO.getUid());
        if (null == user) {
            throw new GlobalException(ExceptionConstant.USER_NOT_EXIST);
        }

        if (StringUtils.isEmpty(accountDTO.getAccountNumber())) {
            throw new GlobalException(ExceptionConstant.ACCOUNT_NUMBER_NULL);
        }

        Account account = accountMapper.queryByAccountNumber(accountDTO.getAccountNumber());
        if (null != account) {
            throw new GlobalException(ExceptionConstant.ACCOUNT_NUMBER_EXIST);
        }

        if (null == accountDTO.getType()) {
            throw new GlobalException(ExceptionConstant.ACCOUNT_TYPE_NULL);
        }

        Account convert = accountConverter.convert(accountDTO);
        convert.setCreateTime(new Date());
        convert.setUpdateTime(new Date());
        int i = accountMapper.insert(convert);
        if (i == 0) {
            return false;
        }
        return true;
    }

    /**
     * 根据id查询账户信息
     * @param id
     * @return
     */
    @Override
    public AccountVO queryAccountById(Long id) {
        Account account = accountMapper.selectById(id);
        return accountConverter.convert(account);
    }

    /**
     * 根据uid查询账户信息
     * @param uid
     * @return
     */
    @Override
    public List<AccountVO> queryAccountListByUid(Long uid) {
        List<Account> accounts = accountMapper.queryAccountListByUid(uid);
        return accountConverter.convert(accounts);
    }

    @Override
    public Page<AccountVO> queryAccountPage(int current, int pageSize) {
        Page<Account> page = new Page<>(current, pageSize);
        Page<Account> accountPage = accountMapper.selectPage(page, null);
        return accountConverter.convert(accountPage);
    }

    @Override
    public List<AccountVO> queryAccountList() {
        List<Account> accounts = accountMapper.selectList(null);
        return accountConverter.convert(accounts);
    }

}




